using System;
using NUnit.Framework;
using ValidationFramework;
using ValidationFramework.Configuration;
using ValidationFramework.Reflection;

namespace ExampleLibraryCSharp.Configuration
{
	public class ConfigurationServiceExample
    {
        
        public class Person
		{
			private static readonly RuntimeMethodHandle setTheAgeHandle =
				typeof(Person).GetMethod("SetTheAge").MethodHandle;


			public string Name
			{
				get;
				set;
			}

			public int Age
			{
				get;
				set;
			}

			public void SetTheAge(int age)
			{
				ParameterValidationManager.ThrowException(this, setTheAgeHandle, age);
				Age = age;
			}
		}

		[TestFixture]
		public class ExampleUsage
		{

			[Test]
			public void Run()
			{
				ConfigurationService.AddAssembly(typeof(ExampleUsage).Assembly);
				var type = typeof(Person);
				var typeDescriptor = TypeCache.GetType(type.TypeHandle);
				Assert.AreEqual(1, typeDescriptor.Properties["Age"].Rules.Count);

				var methodDescriptor =
					MethodCache.GetMethod(type.GetMethod("SetTheAge").MethodHandle);
				Assert.AreEqual(1, methodDescriptor.Parameters["age"].Rules.Count);
			}
		}
	}
}